{include file="header.html"}
<body class="gr">

<div class="navBar">
	<a class="left" href="wap.php?app=member&act=index"><i class="return"></i></a>
	<div class="titleView">优惠券</div>
	<a class="right" href="javascript:void(0)">添加</a>
</div>

<div class="coupon-index main-padding-top">
	<div class="pullRefresh">
		<header class="ge-bottom ge-light">
			<ul class="switchView">
				<li {if isset($status) && $status==1}class="this"{/if}>
					<a href="wap.php?app=coupon&act=index&status=1">有效</a>
				</li>
				<li {if isset($status) && $status==0}class="this"{/if}>
					<a href="wap.php?app=coupon&act=index&status=0">无效</a>
				</li>
			</ul>
		</header>
		
		<ul class="list">
			{foreach from=$data name=g item=g}
			<li class="status{$g->status}">
				<div class="l">
					<div>￥{$g->coupon_money}</div>
					<span>{$g->min_price_memo}</span>
				</div>
				<div class="r">
					<div>{$g->name}<br />{$g->memo}</div>
					<span>有效期至 {$g->end_time}</span>
				</div>
				{if $g->status<0}<div class="t t{$g->status}"></div>{/if}
			</li>
			{/foreach}
		</ul>
	</div>
</div>

{include file="footer.html"}
<script>
var offset = $('.pullRefresh .list > li').length;
function createHtml(g){
	var html = '<li class="status'+g.status+'">\
		<div class="l">\
			<div>￥'+g.coupon_money+'</div>\
			<span>'+g.min_price_memo+'</span>\
		</div>\
		<div class="r">\
			<div>'+g.name+'<br />'+g.memo+'</div>\
			<span>有效期至 '+g.end_time+'</span>\
		</div>\
		'+(g.status!=1?'<div class="t"></div>':'')+'\
	</li>';
	offset++;
	return html;
}
function changePrice(){
	$('.list .l div').priceFont('bigPrice');
}
$(function(){
	changePrice();
	$('.switchView').switchView({ column:'column', index:$('.switchView .this').index() });
	$('.coupon-index').height($.window().height-$('.coupon-index').padding().top);
	$('.navBar .right').click(function(){
		var _this = $(this),
		html = '<div class="coupon-sn" cls="overlay-big" delay-cls="overlay-normal" close-cls="overlay-small">\
			<div class="tip"><span>添加优惠券</span></div>\
			<input type="text" placeholder="请输入优惠券号码" />\
			<a href="javascript:void(0)">确定</a>\
		</div>';
		$.overlay(html, 3, function(){
			$('.coupon-sn a').click(function(){
				var val = $(this).prev().val();
				$.postJSON('api.php?app=coupon&act=add', { sn:val }, function(json){
					if(json.error!=0){ $.overloadError(json.msg);return }
					$.overlay(false);
					$.overloadSuccess('添加成功');
					setTimeout(function(){ location.href=location.href }, 3000);
				});
			});
		});
	});
	$('.pullRefresh').pullRefresh({
		header : true,
		footer : true,
		footerNoMoreText : '- END -',
		refresh : function(fn){
			var _this = this;
			offset = 0;
			$.getJSON('api.php?app=coupon&act=index{if isset($status)}&status={$status}{/if}', function(json){
				if(json.error!=0){ $.overloadError(json.msg);return }
				var html = '';
				if($.isArray(json.data))for(var i=0; i<json.data.length; i++)html += createHtml(json.data[i]);
				_this.find('.list').html(html);
				changePrice();
				fn();
			});
		},
		load : function(fn){
			var _this = this;
			$.getJSON('api.php?app=coupon&act=index{if isset($status)}&status={$status}{/if}', { offset:offset }, function(json){
				if(json.error!=0){ $.overloadError(json.msg);return }
				var html = '';
				if($.isArray(json.data))for(var i=0; i<json.data.length; i++)html += createHtml(json.data[i]);
				_this.find('.list').append(html);
				changePrice();
				fn();
			});
		}
	});
});
</script>